// 通用类型定义
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
  success: boolean
}

// Microi系统API响应类型
export interface MicroiApiResponse<T = any> {
  Code: number
  Data: T
  Msg: string
  DataAppend?: {
    SysMenuHomePage?: SysMenuHomePage
    SysConfig?: SysConfig
    [key: string]: any
  }
  headers?: any
}

export interface RequestConfig {
  url: string
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
  data?: any
  params?: any
  headers?: Record<string, string>
  timeout?: number
  showLoading?: boolean
  loadingText?: string
}

export interface UserInfo {
  id: string
  username: string
  token: string
  refreshToken?: string
  // Microi系统用户信息字段
  No?: string
  Account: string
  Pwd?: string
  Name: string
  DeptId?: string
  DeptName?: string
  DeptCode?: string
  DeptIds?: string
  RoleIds?: string
  CreateTime?: string
  UpdateTime?: string
  Phone?: string
  State?: number
  Remark?: string
  Avatar?: string
  Sex?: string
  Email?: string
  IsDeleted?: number
  Level?: number
  ParentId?: string
  UserId?: string
  UserName?: string
  pr_FATHER_STATION_CODE?: string
  UserType?: string
  TenantName?: string
  TenantId?: string
  PwdEncode?: string
  MiniProgramOpenId?: string
  WxUnionId?: string
  WxOpenId?: string
  _Roles?: RoleInfo[]
  _RoleLimits?: RoleLimitInfo[]
  _IsAdmin?: boolean
  [key: string]: any
}

export interface RoleInfo {
  Id: string
  Name: string
  CreateTime?: string
  UpdateTime?: string
  Sort?: number
  Class?: string
  BaseLimit?: string
  IsDeleted?: number
  Remark?: string
  Level?: number
  DeptIds?: string
  ParentId?: string
  _Child?: any
  SysMenuIds?: any
  SysRoleLimits?: any
  _Test?: any
  DeptNames?: string
  TenantName?: string
  TenantId?: string
}

export interface RoleLimitInfo {
  FkName?: string
  Id: string
  RoleId: string
  FkId: string
  Type: string
  CreateTime?: string
  Customer?: any
  Permission?: string
}

export interface SysConfig {
  Id?: string
  ParentId?: string
  CreateTime?: string
  UpdateTime?: string
  UserId?: string
  UserName?: string
  IsDeleted?: number
  SysTitle?: string
  SysShortTitle?: string
  CompanyName?: string
  SysLogo?: string
  PeizhiMC?: string
  IsEnable?: number
  LoginBgImg?: string
  SysLang?: string
  IsAeroLogin?: number
  ThemeColor?: string
  FaviconIco?: string
  SysLogoType?: string
  SysLogoHeight?: string
  SysLogoLink?: string
  OsClient?: string
  AuthServer?: string
  DomainName?: string
  ClientSecrets?: string
  AccessTokenLifetime?: string
  GlobalV8Code?: string
  FileServer?: string
  MediaServer?: string
  MenuBottomContent?: string
  MenuBg?: string
  MenuBackgroundColor?: string
  MenuWordColor?: string
  PwdShortestLength?: number
  PwdContainNumber?: number
  PwdContainSpecial?: number
  PwdContainUpperLower?: number
  PwdAllowErrorCount?: number
  PwdErrorLockTime?: number
  PwdAllowMultiLogin?: number
  GlobalServerV8Code?: string
  PwdEncode?: string
  EnableChat?: number
  LogoBgColor?: string
  SysTitleColor?: string
  MenuBoxShadow?: string
  MenuWidth?: string
  ActiveMenuBg?: string
  ActiveMenuColor?: string
  DiySystem?: number
  HDFS?: string
  PwdV8?: string
  LoginEndV8Code?: string
  TopWidthFull?: number
  ApiBase?: string
  DefaultIndexUrl?: string
  EnablePrivacyPolicy?: number
  PrivacyPolicyCheck?: number
  PrivacyPolicyName?: string
  PrivacyPolicy?: string
  EnableCaptcha?: number
  UEditorConfig?: string
  CaptchaConfig?: string
  EnableReg?: number
  GetPrivateFileBeforeServerV8?: string
  GetPrivateFileAfterServerV8?: string
  IndexCodeApi?: string
  EnableSwagger?: number
  MiniProgramAudit?: number
  MiniProgramAppId?: string
  MiniProgramSecret?: string
  SmsKey?: string
  SmsSecret?: string
  MiniProgramName?: string
  WeChatMchId?: string
  WxPayPrivateKey?: string
  CertSerialNo?: string
  APIv3SecretKey?: string
  PrintSqlToPage?: number
  ServerVersion?: string
  ClientVersion?: string
  AMapKey?: string
  AMapSecret?: string
  Is_online_office?: number
  BitianYS?: string
  BaiduAK?: string
  SessionAuthTimeout?: string
  YidongDLSJ?: any
  IMSdkAppid?: string
  IMSecretKey?: string
  [key: string]: any
}

export interface SysMenuHomePage {
  Id: string
  Name: string
  Description?: string
  EnName?: string
  EnDescription?: string
  Code: string
  Url: string
  Link?: string
  ParentId: string
  Sort: number
  Icon?: string
  IconClass?: string
  CreateTime?: string
  OpenType?: string
  ComponentName?: string
  ComponentPath?: string
  JquerySelector?: string
  MultRun?: number
  Display?: number
  Class?: string
  UserId?: string
  StoreId?: string
  DiyTableId?: string
  TableDiyFieldIds?: string
  PageTemplate?: string
  SearchFieldIds?: string
  DiyConfig?: string
  SortFieldIds?: string
  SqlWhere?: string
  SqlJoin?: string
  StatisticsFields?: string
  IsDeleted?: number
  DefaultOrderBy?: string
  NotShowFields?: string
  ImportTemplate?: string
  ImportTemplateName?: string
  MoreBtns?: string
  ImportV8?: string
  ExportV8?: string
  ExportMoreBtns?: string
  DetailPageV8?: string
  BatchSelectMoreBtns?: string
  PageBtns?: string
  PageTabs?: string
  InTableEdit?: number
  InTableEditFields?: string
  TableHeaders?: string
  IsMicroiService?: number
  UpdateTime?: string
  UserName?: string
  FormBtns?: string
  SelectFields?: string
  JoinTables?: string
  RoleGroup?: string
  ModuleEngineKey?: string
  ParentIds?: string
  AppDisplay?: number
  DisplayWin?: number
  DisplayMac?: number
  SizeWidthMac?: string
  SizeHeightMac?: string
  ReportName?: string
  ReportId?: string
  EditCodeShowV8?: string
  DelCodeShowV8?: string
  IsChildSystem?: number
  AddCodeShowV8?: string
  [key: string]: any
}

export interface LoginParams {
  username: string
  password: string
  captcha?: string
  _CaptchaId?: string
  _CaptchaValue?: string
}

export interface PageParams {
  page: number
  pageSize: number
  total?: number
}

export interface ListResponse<T> {
  list: T[]
  total: number
  page: number
  pageSize: number
}

// 路由相关类型
export interface RouteConfig {
  path: string
  name?: string
  meta?: {
    title?: string
    requiresAuth?: boolean
    roles?: string[]
    permissions?: string[]
    keepAlive?: boolean
    showTabBar?: boolean
  }
}

export interface RouteGuard {
  beforeEnter?: (
    to: RouteConfig,
    from?: RouteConfig
  ) => boolean | Promise<boolean>
  afterEnter?: (to: RouteConfig, from?: RouteConfig) => void
}

// 错误处理相关类型
export enum ErrorType {
  NETWORK = 'NETWORK',
  API = 'API',
  VALIDATION = 'VALIDATION',
  PERMISSION = 'PERMISSION',
  SYSTEM = 'SYSTEM',
  UNKNOWN = 'UNKNOWN',
}

export interface ErrorInfo {
  type: ErrorType
  message: string
  code?: string | number
  stack?: string
  timestamp: number
  url?: string
  userAgent?: string
  userId?: string
  extra?: Record<string, any>
}

// 国际化相关类型
export type Language = 'zh-CN' | 'en-US'
export type TranslationKey = string
export type TranslationValue = string | Record<string, any>

export interface TranslationConfig {
  language: Language
  fallbackLanguage: Language
  translations: Record<Language, Record<TranslationKey, TranslationValue>>
  pluralization?: Record<Language, (count: number) => string>
}

// 调试相关类型
export enum LogLevel {
  DEBUG = 0,
  INFO = 1,
  WARN = 2,
  ERROR = 3,
  NONE = 4,
}

export interface LogConfig {
  level: LogLevel
  enableConsole: boolean
  enableStorage: boolean
  maxStorageSize: number
  enableRemote: boolean
  remoteUrl?: string
  enableTimestamp: boolean
  enableCaller: boolean
}

export interface LogEntry {
  level: LogLevel
  message: string
  data?: any
  timestamp: number
  caller?: string
  stack?: string
}

// 性能相关类型
export interface PerformanceEntry {
  name: string
  duration: number
  timestamp: number
  type: 'function' | 'api' | 'render' | 'custom'
}

// 安全相关类型
export type EncryptionAlgorithm = 'AES-256-GCM' | 'AES-256-CBC' | 'RSA-OAEP'

export interface SecurityConfig {
  enableEncryption: boolean
  encryptionKey: string
  enableXSSProtection: boolean
  enableDataMasking: boolean
  allowedOrigins: string[]
}
